package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by charlieg on 21/07/2014.
 */
public class Library {
    private ArrayList<Book> books = new ArrayList<Book>(); //Creates an array of books.

    public void start(){ //Method to start the app.
        setup();
        welcomeScreen();
        menu();
    }

    public void setup(){ //Creates books.
        Book book1 = new Book("The stranger", "Albert Camus");
        Book book2 = new Book("To Kill a Mockingbird", "Harper Lee");

        addBook(book1);
        addBook(book2);
    }

    public void addBook(Book book){ //Adds the books to the ArrayList of books.
        books.add(book);
    }

    public Book getBook(int index){ //Method to borrow the books.
        return books.get(index - 1);
    }

    public void welcomeScreen(){
        System.out.println("**************************");
        System.out.println("* Welcome to Biblioteca! *");
        System.out.println("**************************");
    }

    public void setUpMenu(){
        System.out.println("*           Menu         *");
        System.out.println("*    =================   *");
        System.out.println("*    1. List of Books    *");
        System.out.println("*    2. Check out book   *");
        System.out.println("*    3. Return book      *");
        System.out.println("*    4. Quit             *");
        System.out.println("*    =================   *");
        System.out.println("Please enter your selection");
    }

    String selection;

    public void menu(){ //Loop for the menu.

        while(selection != "4"){
            setUpMenu();
            selection = getMenuSelection(); //Get the input
            selectMenuOption(selection); //Display the right menu option.
        }
    }

    public void selectMenuOption(String selection){
        if(Integer.parseInt(selection) == 1){
            showBooks();
        } else if(Integer.parseInt(selection) == 2){
            System.out.println("Which book would you like to borrow?");
            int bookChoice = getNumberInput();
            checkOutBook(bookChoice);
        } else if(Integer.parseInt(selection) == 3){
            System.out.println("Which book would you like to return?");
            int returnBookChoice = getNumberInput();
            returnBook(returnBookChoice);
        } else if(Integer.parseInt(selection) == 4){
            System.out.println("Quit");
            System.exit(0);
        } else{
            System.out.println("Select a valid option!");
        }
    }

    public void checkOutBook(int bookChoice){ //Borrow a book
        Book book;

        if(bookChoice <= 0 || bookChoice > books.size()){ //If the book you want to borrow is not in the list, display the error message.
            System.out.println("Please select a valid book");
            return;
        }

        book = getBook(bookChoice);
        if(book.isAvailable()){
            book.setAvailable(false); //If the book is available, display the thank you message.
            System.out.println("Thank you! Enjoy the book!");
        } else{
            System.out.println("That book is not available"); //Otherwise display the error message.
        }
    }

    public String getMenuSelection(){ //Get the input from the user.
        Scanner in = new Scanner(System.in);
        return in.next().toLowerCase();
    }

    public int getNumberInput(){
        Scanner in = new Scanner(System.in);
        return in.nextInt();
    }

    public void showBooks(){
        int index =  1;

        for (Book book : books){
            if(book.isAvailable() == true) { //Show the available books. Not quite working.
                System.out.println(index + "." + book.getTitle() + " by " + book.getAuthor());
                index++;
            } else {
                System.out.println(book);
                return;
            }
        }
    }

    public void returnBook(int returnBookChoice){  //Same as method to borrow books.
        Book book;

        if(returnBookChoice <= 0 || returnBookChoice > books.size()){
            System.out.println("That is not a valid book to return.");
            return;
        }

        book = getBook(returnBookChoice);
        if(book.isAvailable() == false){
            book.setAvailable(true);
            System.out.println("Thank you for returning the book.");
        } else{
            System.out.println("That is not a valid book to return.");
        }

    }
}
